package com.xiaoyang.forum.controller;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.util.StrUtil;
import com.xiaoyang.forum.common.code.BaseController;
import com.xiaoyang.forum.common.code.Result;
import com.xiaoyang.forum.common.code.ResultCode;
import com.xiaoyang.forum.common.util.JWTUtil;
import com.xiaoyang.forum.domain.dto.UserEmailDTO;
import com.xiaoyang.forum.domain.dto.UserPasswordDTO;
import com.xiaoyang.forum.domain.dto.UserUpdateDTO;
import com.xiaoyang.forum.domain.vo.UserDetailsVO;
import com.xiaoyang.forum.domain.vo.UserVO;
import com.xiaoyang.forum.domain.dto.UserDTO;
import com.xiaoyang.forum.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: XiaoYang
 * @CreateTime: 2025-08-10
 * @Description: 用户接口
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserDTO userDTO) {
        log.info("用户注册", userDTO.getUsername());
        // 校验参数
        if (StringUtil.isNullOrEmpty(userDTO.getUsername())
                || StringUtil.isNullOrEmpty(userDTO.getPassword())) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        userService.registerUser(userDTO.getUsername(), userDTO.getPassword());
        return Result.success();
    }

    /**
     * 用户登录--账号密码
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserDTO userDTO) {
        log.info("用户登录", userDTO.getUsername());
        // 1. 校验参数
        if (StringUtil.isNullOrEmpty(userDTO.getUsername())
                || StringUtil.isNullOrEmpty(userDTO.getPassword())) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        return Result.success(ResultCode.SUCCESS.getMessage(),
                userService.login(userDTO.getUsername(), userDTO.getPassword()));
    }

    /**
     * 发送验证码
     * @param
     * @return
     */
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserEmailDTO userEmailDTO) {
        log.info("发送验证码");
        return toR(userService.sendCode(userEmailDTO.getEmail()));
    }

    /**
     * 验证码登录
     * @param userEmailDTO
     * @return
     */
    @PostMapping("code/login")
    public Result<String>codeLogin(@RequestBody UserEmailDTO userEmailDTO) {
        log.info("用户登录{}", userEmailDTO.getEmail());
        return Result.success(ResultCode.SUCCESS.getMessage(),
                userService.codeLogin(userEmailDTO.getEmail(), userEmailDTO.getCode()));
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public Result<UserVO> getUserInfo(@RequestHeader String token) {
        log.info("获取用户信息", token);
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        return Result.success(userService.getUserInfo(token));
    }

    /**
     * 退出登录 todo
     * @param token
     * @return
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader String token) {
        return null;
    }

    /**
     * 个人主页
     * @param userId
     * @return
     */
    @GetMapping("/semiLogin/getUserAndArticleInfo")
    public Result<UserDetailsVO> getUserAndArticleInfo(Long userId) {
        log.info("获取用户信息", userId);
        if (userId == null) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        return Result.success(userService.getUserAndArticleInfo(userId));
    }

    /**
     * 修改用户信息
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/modifyUserInfo")
    public Result<Void> modifyUserInfo(@RequestHeader String token,
                                       @RequestBody(required = false) UserUpdateDTO userUpdateDTO) {
        // 1. 校验用户是否登录
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        // 校验参数
        if (userUpdateDTO == null) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        return toR(userService.modifyUserInfo(userId, userUpdateDTO));
    }

    /**
     * 修改密码
     * @param userPasswordDTO
     * @return
     */
    @PutMapping("/modifyPassword")
    public Result<Void> modifyPassword(@RequestHeader String token,
                                       @RequestBody UserPasswordDTO userPasswordDTO) {
        // 1. 校验用户是否登录
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return toR(userService.modifyPassword(userId, userPasswordDTO));
    }

    /**
     * 修改头像
     * @param token
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestHeader String token,
                                        @RequestBody UserUpdateDTO userUpdateDTO) {
        // 1. 校验用户是否登录
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return toR(userService.updateHeadImage(userUpdateDTO.getAvatarUrl(), userId));
    }
}
